package com.quanyan.stadium.service.gameEventOrder.impl;

import com.quanyan.common.Plugin.Page;
import com.quanyan.common.exception.BizException;
import com.quanyan.orderpay.response.RespPayInfo;
import com.quanyan.stadium.api.req.v2.ReqGameEventOrder;
import com.quanyan.stadium.api.resp.v2.RespGameEventOrder;
import com.quanyan.stadium.entity.TbGameOrder;
import com.quanyan.stadium.entity.TbGameOrderExample;
import com.quanyan.stadium.entity.vo.GameEvent;
import com.quanyan.stadium.mapper.TbGameOrderMapper;
import com.quanyan.stadium.service.commonComponent.BaseService;
import com.quanyan.stadium.service.commonComponent.OfficialGameCache;
import com.quanyan.stadium.service.gameEventOrder.GameEventOrderService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2017/3/3.
 */
@Service
public class GameEventOrderServiceImpl implements GameEventOrderService {

    @Autowired
    TbGameOrderMapper tbGameOrderMapper;

    @Autowired
    OfficialGameCache officialGameCache;

    @Autowired
    BaseService baseService;

    @Override
    public List<RespGameEventOrder> getGameEventOrderList(ReqGameEventOrder reqGameEventOrder) {
        List<RespGameEventOrder> result = new ArrayList<>();
        List<TbGameOrder> orderList = this.getGameOrderList(reqGameEventOrder);
        if(orderList == null || orderList.size() == 0){
            return result;
        }

        /**
         * 查询支付方式
         */
        List<RespPayInfo> payInfos = baseService.queryPayTypeList();

        for(TbGameOrder order : orderList){
            try {
                GameEvent gameEvent = officialGameCache.getGameEvent(order.getGameEventId());
                RespGameEventOrder respGameEventOrder = new RespGameEventOrder();
                BeanUtils.copyProperties(gameEvent,respGameEventOrder);
                BeanUtils.copyProperties(order,respGameEventOrder);
                respGameEventOrder.setAddress(gameEvent.getPlaceInfo().getAddress());
                respGameEventOrder.setPayInfos(payInfos);
                result.add(respGameEventOrder);
            }catch (Exception e){
                throw new BizException("查询赛事出错！");
            }
        }
        return result;
    }

    private List<TbGameOrder> getGameOrderList(ReqGameEventOrder reqGameEventOrder){
        TbGameOrderExample example = new TbGameOrderExample();
        TbGameOrderExample.Criteria criteria = example.createCriteria();
        criteria.andUidEqualTo(reqGameEventOrder.getUid());
        criteria.andGameOrderNoIsNotNull();
        criteria.andOrderAmountGreaterThan(new BigDecimal(0));
        example.setOrderByClause(" create_time desc ");
        int pageSize = reqGameEventOrder.getPageSize();
        int pageNo = reqGameEventOrder.getPageNo();
        example.setPage(new Page(pageSize * pageNo, pageSize));
        return tbGameOrderMapper.selectByExample(example);
    }


}
